<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>岗位管理</span>
      </div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="岗位编码">
          <el-input v-model="searchForm.postCode" placeholder="请输入岗位编码" clearable size="small"></el-input>
        </el-form-item>
        <el-form-item label="岗位名称">
          <el-input v-model="searchForm.postName" placeholder="请输入岗位名称" clearable size="small"></el-input>
        </el-form-item>
        <el-form-item label="状态：">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable size="small">
            <el-option label="正常" value="0"></el-option>
            <el-option label="停用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="doSearch">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="box-card">
      <el-button type="primary" style="margin-bottom: 10px" @click="handleAdd" v-hasPermi="'system:post:add'">添加
      </el-button>
      <el-table :data="pageData.data" style="width: 100%" border>
        <el-table-column prop="postId" label="编号" width="80px"></el-table-column>
        <el-table-column prop="postCode" label="岗位编码"></el-table-column>
        <el-table-column prop="postName" label="岗位名称"></el-table-column>
        <el-table-column prop="postSort" label="岗位排序"></el-table-column>
        <el-table-column prop="status" label="状态" width="80px">
          <template slot-scope="scope">
            {{ scope.row.status == 1 ? "停用" : "正常" }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作" width="240px">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" v-hasPermi="'system:post:edit'">
              编辑
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)"
                       v-hasPermi="'system:post:remove'">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageData.pageNo"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageData.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageData.totalCount">
      </el-pagination>

      <el-dialog :title="saveForm.postId==null?'新增岗位':'修改岗位'" :visible.sync="dialogFormVisible">
        <el-form :model="saveForm" :rules="rules" ref="saveForm" label-width="90px">
          <el-form-item label="岗位名称" prop="postName">
            <el-input v-model="saveForm.postName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="岗位编码" prop="postCode">
            <el-input v-model="saveForm.postCode" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="岗位序号" prop="postSort">
            <el-input-number v-model="saveForm.postSort" autocomplete="off" controls-position="right"></el-input-number>
          </el-form-item>
          <el-form-item label="状态">
            <el-radio-group v-model="saveForm.status">
              <el-radio label="0">正常</el-radio>
              <el-radio label="1">停用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea" v-model="saveForm.remark" autocomplete="off" style="width:520px"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="doSave">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //查询的数据模型
      searchForm: {
        //过滤条件
        postCode: null,
        postName: null,
        status: null,

        //分页条件
        pageNo: 1,
        pageSize: 5,
      },
      //分页数据模型
      pageData: {
        data: [], // 分页数据
        pageNo: 1, // 当前页
        pageSize: 5, // 一页的大小
        totalCount: 0, // 总数据量
        totalPageCount: 0, // 总页数
      },
      //控制对话框显示还是隐藏
      dialogFormVisible: false,
      //新增的数据模型
      saveForm: {
        postId: null,
        postCode: null,
        postName: null,
        status: "0",
        postSort: null,
        remark: null,
      },
      //验证数据模型
      rules: {
        postCode: [
          {required: true, message: "请输入岗位编码！", trigger: "blur"},
        ],
        postName: [
          {required: true, message: "请输入岗位名称！", trigger: "blur"},
        ],
        postSort: [
          {required: true, message: "请输入序号！", trigger: "blur"},
        ],
      },
    };
  },
  created() {
    this.doSearch();
  },
  methods: {
    //查询用户信息
    doSearch() {
      this.$axios.get("/api/sys/post/list", {params: this.searchForm})
          .then((res) => {
            if (res.data.code == 200) {
              this.pageData.pageNo = res.data.data.current;
              this.pageData.pageSize = res.data.data.size;
              this.pageData.totalCount = res.data.data.total;
              this.pageData.totalPageCount = res.data.data.pages;
              this.pageData.data = res.data.data.records;
            } else {
              this.$message.error(res.data.msg);
            }
          })
          .catch((err) => {
            console.log(err);
          });
    },
    //改变每页显示的条数
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.doSearch();
    },
    //改变当前的页码数
    handleCurrentChange(val) {
      this.searchForm.pageNo = val;
      this.doSearch();
    },
    //清空新增表单数据
    reset() {
      this.saveForm = {
        postId: null,
        postCode: null,
        postName: null,
        status: "0",
        postSort: null,
        remark: null,
      };
    },
    //添加
    handleAdd() {
      //1.清空模型数据
      this.reset();
      //2.显示对话框
      this.dialogFormVisible = true;
    },
    //保存
    doSave() {
      this.$refs["saveForm"].validate((vali) => {
        if (vali) {
          //发送ajax请求
          this.$axios.post("/api/sys/post/save", this.saveForm)
              .then((res) => {
                if (res.data.code == 200) {
                  //1.关闭对话框
                  this.dialogFormVisible = false;
                  //2.友好提示
                  this.$message.success(res.data.msg);
                  //3.刷新表格
                  this.doSearch();
                } else {
                  //1.友好提示
                  this.$message.error(res.data.msg);
                }
              })
              .catch((err) => {
                console.log(err);
              });
        }
      });
    },
    //编辑
    handleEdit(index, row) {
      //1.清空模型数据
      this.reset();
      //2.查询要修改的数据
      this.$axios.get("/api/sys/post/view/" + row.postId).then((res) => {
        if (res.data.code == 200) {
          this.saveForm = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch((err) => {
        console.log(err);
      })
      //3.显示对话框
      this.dialogFormVisible = true;
    },
    //删除
    handleDelete(index, row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //1.发送ajax请求
        this.$axios.delete("/api/sys/post/del/" + row.postId).then((res) => {
          if (res.data.code == 200) {
            //1.友好提示
            this.$message.success(res.data.msg);
            //2.刷新表格
            this.doSearch();
          } else {
            //1.友好提示
            this.$message.error(res.data.msg);
          }
        }).catch((err) => {
          console.log(err);
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  },
}
</script>

<style scoped>
</style>

